package com.wbdgj.ui.home;

import android.content.Context;
import android.content.Intent;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.TextView;

import com.amap.api.location.AMapLocation;
import com.amap.api.location.AMapLocationClient;
import com.amap.api.location.AMapLocationClientOption;
import com.amap.api.location.AMapLocationListener;
import com.bumptech.glide.Glide;
import com.google.gson.internal.LinkedTreeMap;
import com.igexin.sdk.PushManager;
import com.wbdgj.R;
import com.wbdgj.adapter.WbAdapter;
import com.wbdgj.base.BaseActivity;
import com.wbdgj.base.BaseApplication;
import com.wbdgj.base.BaseWhiteActivity;
import com.wbdgj.http.HttpAdapter;
import com.wbdgj.http.OnResponseListener;
import com.wbdgj.model.ResultObjectModel;
import com.wbdgj.ui.login.LoginActivity;
import com.wbdgj.utils.CommonUtils;
import com.wbdgj.utils.SpKeyUtils;
import com.wbdgj.utils.ToastUtils;
import com.wbdgj.utils.refresh.PullListView;
import com.wbdgj.utils.refresh.PullToRefreshLayout;

import java.text.DecimalFormat;
import java.util.ArrayList;

import butterknife.BindView;
import jp.wasabeef.glide.transformations.BlurTransformation;
import retrofit2.Call;

public class ZdDetailActivity extends BaseWhiteActivity{

    private String id, COORDSX, COORDSY;
    private Context context = this;

    @BindView(R.id.zdBImg)
    ImageView zdBImg;
    @BindView(R.id.zdImg)
    ImageView zdImg;
    @BindView(R.id.name)
    TextView name;
    @BindView(R.id.content)
    TextView content;
    @BindView(R.id.total)
    TextView total;

    @BindView(R.id.demo_listview)
    PullListView myPullListView;
    @BindView(R.id.pullToRefreshLayout)
    PullToRefreshLayout myPullToRefreshLayout;
    private int page = 1;
    private int totalPage;
    private ArrayList<LinkedTreeMap> linkedTreeMapList;
    private WbAdapter wbAdapter;
    private DecimalFormat decimalFormat;

    //声明AMapLocationClient类对象
    public AMapLocationClient mLocationClient = null;
    //声明AMapLocationClientOption对象
    public AMapLocationClientOption mLocationOption = null;
    @Override
    protected int getLayoutResId() {
        return R.layout.ui_zd_detail;
    }

    @Override
    protected void initView() {
        decimalFormat = new DecimalFormat("###################.###########");
        id = getIntent().getStringExtra("id");
        queryAll("", "", "", "", "2");
        listAll();
        findViewById(R.id.btn_back).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });

        myPullToRefreshLayout.setOnRefreshListener(new PullToRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh(PullToRefreshLayout pullToRefreshLayout) {
                pullToRefreshLayout.postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        page = 1;
                        queryAll("", "", "", "", "2");
                        myPullToRefreshLayout.refreshFinish(true);
                        ToastUtils.toastShort("刷新成功");
                    }
                }, 1000);
            }

            @Override
            public void onLoadMore(PullToRefreshLayout pullToRefreshLayout) {
                page = page + 1;
                if (page > totalPage){
                    ToastUtils.toastShort("已无更多");
                }else{
                    queryAll("", "", "", "", "2");
                }
                myPullToRefreshLayout.loadMoreFinish(true);
            }
        });
        myPullListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                if (!wbAdapter.hasNoData){
                    if (!(wbAdapter.getItem(i).get("DISTANCE")+"").equals("null")){
                        startActivity(new Intent(context, WbDetailActivity.class).putExtra("id", decimalFormat.format(wbAdapter.getItem(i).get("BRANCH_ID"))).putExtra("DISTANCE", decimalFormat.format(wbAdapter.getItem(i).get("DISTANCE"))));
                    }else{
                        startActivity(new Intent(context, WbDetailActivity.class).putExtra("id", decimalFormat.format(wbAdapter.getItem(i).get("BRANCH_ID"))).putExtra("DISTANCE", ""));
                    }
                }
            }
        });
    }


    private void listAll() {
        Call<ResultObjectModel> call = HttpAdapter.getSerives().getBossUser(BaseApplication.getInstance().getSpUtil().getString(SpKeyUtils.TOKEN), id);
        call.enqueue(new OnResponseListener<ResultObjectModel>(context) {
            @Override
            protected void onSuccess(ResultObjectModel userModelBaseResultModel) {
                if(userModelBaseResultModel.getResultCode().equals("0000")){
                    final DecimalFormat decimalFormat = new DecimalFormat("###################.###########");
                    LinkedTreeMap linkedTreeMap = (LinkedTreeMap) userModelBaseResultModel.getData();
                    if (!(linkedTreeMap.get("LOGO")+"").equals("null")){
                        Glide.with(context).load(HttpAdapter.HEAD_URL+linkedTreeMap.get("LOGO")).error(R.mipmap.home_wk_01).into(zdImg);
                    }
                    Glide
                            .with(context)
                            .load(HttpAdapter.HEAD_URL+linkedTreeMap.get("LOGO"))
                            .error(R.mipmap.home_wk_01)
                            .centerCrop()
                            .crossFade()
                            .dontAnimate()
                            .bitmapTransform(new BlurTransformation(context,23,4))
                            .into(zdBImg);

//                    name.setText(linkedTreeMap.get("LOGO")+"");
                    if (!(linkedTreeMap.get("REMARK")+"").equals("null")){
                        content.setText(linkedTreeMap.get("REMARK")+"");
                    }
                    if (!(linkedTreeMap.get("NAME")+"").equals("null")){
                        name.setText(linkedTreeMap.get("NAME")+"");
                    }

                }else if (userModelBaseResultModel.getResultCode().equals("1111")){
                    ToastUtils.toastShort("账号没有登录或在其它地方登录");
                    BaseApplication.getInstance().getSpUtil().putString(SpKeyUtils.TOKEN, "");
                    BaseApplication.getInstance().getSpUtil().putString(SpKeyUtils.IS_AUTO_LOGIN, "2");
                    PushManager.getInstance().unBindAlias(context, BaseApplication.getInstance().getSpUtil().getString(SpKeyUtils.LOGIN_PHONE), false, BaseApplication.getInstance().getSpUtil().getString(SpKeyUtils.CID));
                    Intent intent = new Intent(context, LoginActivity.class);
                    intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK);
                    startActivity(intent);
                }else{
                    ToastUtils.toastShort(userModelBaseResultModel.getResultMsg());
                }
            }
        });
    }

    private void queryAll(String PROVINCE, String CITY, String DISTRICT, String DISTANCE, String AI) {
        Call<ResultObjectModel> call = HttpAdapter.getSerives().queryAll(BaseApplication.getInstance().getSpUtil().getString(SpKeyUtils.TOKEN), BaseApplication.getInstance().getSpUtil().getString(SpKeyUtils.COORDSX), BaseApplication.getInstance().getSpUtil().getString(SpKeyUtils.COORDSY), "10", page+"",
                PROVINCE, CITY, DISTRICT, "", DISTANCE, AI, id);
        call.enqueue(new OnResponseListener<ResultObjectModel>(context) {
            @Override
            protected void onSuccess(ResultObjectModel userModelBaseResultModel) {
                if(userModelBaseResultModel.getResultCode().equals("0000")){
                    LinkedTreeMap linkedTreeMap = (LinkedTreeMap) userModelBaseResultModel.getData();
                    DecimalFormat decimalFormat = new DecimalFormat("###################.###########");
                    LinkedTreeMap pageLinkedTreeMap = (com.google.gson.internal.LinkedTreeMap) linkedTreeMap.get("page");
                    totalPage = Integer.parseInt(decimalFormat.format(pageLinkedTreeMap.get("totalPage")));
                    linkedTreeMapList = new ArrayList<>();
                    linkedTreeMapList = (ArrayList<LinkedTreeMap>) linkedTreeMap.get("branchList");
                    total.setText(decimalFormat.format(pageLinkedTreeMap.get("totalCount"))+"家门店");
                    if (page == 1){
                        wbAdapter = new WbAdapter(context, linkedTreeMapList);
                        myPullListView.setAdapter(wbAdapter);

                    }else{
                        wbAdapter.loadMoreListView(linkedTreeMapList);
                        wbAdapter.notifyDataSetChanged();
                    }
                }else if (userModelBaseResultModel.getResultCode().equals("1111")){
                    ToastUtils.toastShort("账号没有登录或在其它地方登录");
                    BaseApplication.getInstance().getSpUtil().putString(SpKeyUtils.TOKEN, "");
                    BaseApplication.getInstance().getSpUtil().putString(SpKeyUtils.IS_AUTO_LOGIN, "2");
                    Intent intent = new Intent(context, LoginActivity.class);
                    intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK);
                    startActivity(intent);
                }else{
                    ToastUtils.toastShort(userModelBaseResultModel.getResultMsg());
                }
            }
        });
    }
}
